<%@page import="kr.go.nih.biomedi.pSEVA.bean.SNP132"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="java.io.File"%>
<%@page import="java.util.List"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>::::pSEVA:::: Batch Run</title>
		<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
	</head>
	<body>
			<jsp:include page="../../top.jsp" flush="false"/>
			<jsp:useBean id="prinSeq" class="kr.go.nih.biomedi.pSEVA.bean.PrinSeq"/>
			<jsp:useBean id="renseqqcfile" class="kr.go.nih.biomedi.pSEVA.bean.RenSeqQCFile"/>
			<jsp:useBean id="bwa" class="kr.go.nih.biomedi.pSEVA.bean.BWA"/>
			<jsp:useBean id="samstat" class="kr.go.nih.biomedi.pSEVA.bean.Samstat"/>
			<jsp:useBean id="samtools" class="kr.go.nih.biomedi.pSEVA.bean.Samtools"/>
			<jsp:useBean id="run" class="kr.go.nih.biomedi.pSEVA.bean.RuntimeThread"/>
			<jsp:useBean id="writescript" class="kr.go.nih.biomedi.pSEVA.bean.WriteScript"/>
			<jsp:useBean id="runscript" class="kr.go.nih.biomedi.pSEVA.bean.RunScript"/>
			<jsp:useBean id="variantannotation" class="kr.go.nih.biomedi.pSEVA.bean.VariantAnnotation"/>
			<div id="resultPanel">
			<%
				/* configure section */
				String saveFolder = "/home/chris/temp/filesave";
				String encType = "utf-8";
				int count = 0;
				int maxSize = 500 * 1024 * 1024;
				ServletContext context = getServletContext();
				//
				
				/* options Section */
				String inputOption = null;
				String mapperOption = null;
				String genomeReferenceOption = null;
				String nodeNumberOption = null;
				String seqTypeOption;
				ArrayList<String> fileList = new ArrayList<String>();
				ArrayList<String> originFileList = new ArrayList<String>();
				//
				
				/* result section */
				String SeqQCResult;
				String MappingBwaAlnResult;
				String MappingBwaSamseResult;
				String MappingQCResult;
				String VcallSamtoolsBamResult;
				String VcallSamtoolsSortResult;
				String VcallSamtoolsPileupResult;
				String VcallSamtoolsSnpcallResult;								
				
				try {
					/* directory and file upload location check */
					File isDir = new File(saveFolder);
					if (!isDir.isDirectory()) isDir.mkdir();
					//
					MultipartRequest multi = new MultipartRequest(request, saveFolder, maxSize, encType, new DefaultFileRenamePolicy());
					/* get Parameter List */
					Enumeration params = multi.getParameterNames();
					while(params.hasMoreElements()) {
						String name = (String) params.nextElement();
						String parameter = multi.getParameter(name);
						
						if (name.equals("inputOption")) inputOption = parameter;
						if (name.equals("mapper")) mapperOption = parameter;
						if (name.equals("reference")) genomeReferenceOption = parameter;
						if (name.equals("node_num")) nodeNumberOption = parameter;
						if (name.equals("seq_type")) seqTypeOption = parameter;
					}
					//
					
					/* get Sequence file Name*/ 
					Enumeration files = multi.getFileNames();
					while(files.hasMoreElements()) {
						String name = (String)files.nextElement();
						String inputFile = multi.getFilesystemName(name);
						String originalFile = multi.getOriginalFileName(name);
						fileList.add(inputFile);
						originFileList.add(originalFile);
					}
					
					/* Single Node */
					if(nodeNumberOption.equals("sn")){					
						for (int i = 0; i < fileList.size() && i < originFileList.size(); i++) {
				%>
							<h3>Sequence : <%=fileList.get(i)%> / <%=originFileList.get(i)%></h3>
		
							<!-- Seq-QC -->
							<jsp:setProperty name="renseqqcfile" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="renseqqcfile" property="inputFile" value="<%=fileList.get(i)%>"/>
														
							<jsp:setProperty name="run" property="cmd" value="<%=renseqqcfile.doRenOrgSeq()%>"/>
							<p>Set command : <jsp:getProperty property="cmd" name="run"/>
							<%=run.execute()%>
							
							<jsp:setProperty name="prinSeq" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="prinSeq" property="inputFile" value="<%=renseqqcfile.getNewInputFile()%>"/>
							<jsp:setProperty name="prinSeq" property="inputOption" value="<%=inputOption%>"/>
							<jsp:setProperty name="run" property="cmd" value="<%=prinSeq.doPrinSeqExecute()%>"/>
							<%SeqQCResult=run.execute();%>
							
							<jsp:setProperty name="run" property="cmd" value="<%=renseqqcfile.doCpOrgSeq()%>"/>
							<%=run.execute()%>
							<jsp:setProperty name="run" property="cmd" value="<%=renseqqcfile.doCpSeqQCResult()%>"/>
							<%=run.execute()%>							
														
							<% if (SeqQCResult.equals("true")) {%>
										<font>1. Seq-QC</font> &nbsp; <input type="button" name="seqqc" value="view" onclick="onSeqQC()"> &nbsp;&nbsp;
							<% } else { %>
										<font>1. Seq-QC</font> &nbsp; <input type="button" name="seqqc" value="view" disabled="disabled"> &nbsp;&nbsp;
							<% } %>
							
							<!-- Mapping -->
							<jsp:setProperty name="bwa" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="bwa" property="inputFile" value="<%=fileList.get(i)%>"/>
							<!-- Return command : <%=bwa.doBWAalnExecute()%>  -->
							<jsp:setProperty name="run" property="cmd" value="<%=bwa.doBWAalnExecute()%>"/>					
							<%MappingBwaAlnResult=run.execute();%>
							
							<jsp:setProperty name="run" property="cmd" value="<%=bwa.doBWAsamseExecute()%>"/>
							<%MappingBwaSamseResult=run.execute();%>
							
							<%if (MappingBwaAlnResult.equals("true") && MappingBwaSamseResult.equals("true") ) {%>
										<font>2. Mapping</font> &nbsp; <input type="button" name="mapping" value="view"> &nbsp;&nbsp;
							<% } else { %>
										<font>2. Mapping</font> &nbsp; <input type="button" name="mapping" value="view" disabled="disable"> &nbsp;&nbsp;
							<% } %>
							
							<!-- Mapping-QC -->
							<jsp:setProperty name="samstat" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="samstat" property="inputFile" value="<%=fileList.get(i)%>"/>
							<jsp:setProperty name="run" property="cmd" value="<%=samstat.doSamstatExecute()%>"/>
												
							<%MappingQCResult=run.execute();%>
							<% if (MappingQCResult.equals("true")) {%>
										<font>3. Mapping-QC</font> &nbsp; <input type="button" name="mappingqc" value="view"> &nbsp;&nbsp;
							<% } else { %>
										<font>3. Mapping-QC</font> &nbsp; <input type="button" name="mappingqc" value="view" disabled="disable"> &nbsp;&nbsp;
							<% } %>
							
							<!-- Variant Call -->
							<jsp:setProperty name="samtools" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="samtools" property="inputFile" value="<%=fileList.get(i)%>"/>
							<jsp:setProperty name="run" property="cmd" value="<%=samtools.doSamtoolsBAMExecute()%>"/>					
							<%VcallSamtoolsBamResult=run.execute();%>
							<jsp:setProperty name="run" property="cmd" value="<%=samtools.doSamtoolsSortExecute()%>"/>
							<%VcallSamtoolsSortResult=run.execute();%>
							<jsp:setProperty name="run" property="cmd" value="<%=samtools.doSamtoolsPileupExecute()%>"/>
							<%VcallSamtoolsPileupResult=run.execute();%>
							<jsp:setProperty name="run" property="cmd" value="<%=samtools.doSamtoolsSNPcallExecute()%>"/>
							<%VcallSamtoolsSnpcallResult=run.execute();%>
							
							<%if (VcallSamtoolsBamResult.equals("true") && VcallSamtoolsSortResult.equals("true") && 
									VcallSamtoolsPileupResult.equals("true") && VcallSamtoolsSnpcallResult.equals("true")) {%>
										<font>4. Variant Call</font> &nbsp; <input type="button" name="vcall" value="view"> &nbsp;&nbsp;
							<% } else { %>
										<font>4. Variant Call</font> &nbsp; <input type="button" name="vcall" value="view" disabled="disable"> &nbsp;&nbsp;
							<% } %>
		
							<!-- Variant Annotation -->
							<font>5. Variant Annotation</font> &nbsp; <input type="button" name="vannotation" value="view"><br>
							<jsp:setProperty name="variantannotation" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="variantannotation" property="inputFile" value="<%=fileList.get(i)%>"/>
							<%=variantannotation.doExecute()%>
							<table>
								<tr>
									<td>chrom</td>
									<td>chromStart</td>
									<td>chromEnd</td>
								</tr>
							<%
									System.out.println(variantannotation.getSNPList().size());
									List<SNP132> snpList = (List<SNP132>) variantannotation.getSNPList();
									System.out.println(snpList.size());
									for (int t = 0; t < snpList.size(); t++) {
										SNP132 tmp = (SNP132) snpList.get(t);
							%>
								<tr>
										<td><%=tmp.getChrom()%></td>
										<td><%=tmp.getChromStart()%></td>
										<td><%=tmp.getChromEnd()%></td>
										<td><%=tmp.getName()%></td>
										<td><%=tmp.getObserved()%></td>
										<td><%=tmp.getMoltype()%></td>
										<td><%=tmp.getClasstype()%></td>
										<td><%=tmp.getValid()%></td>
								</tr>
							<%
									}
							%>
							</table>
			<%
						}
					/* Multi Node */	
					}else if(nodeNumberOption.equals("mn")){
					
						/* Writing scripts for SGE */
						for (int i = 0; i < fileList.size() && i < originFileList.size(); i++) {
			%>
							<!-- Seq-QC -->
							<jsp:setProperty name="prinSeq" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="prinSeq" property="inputFile" value="<%=fileList.get(i)%>"/>
							<jsp:setProperty name="prinSeq" property="inputOption" value="<%=inputOption%>"/>
							
							<jsp:setProperty name="writescript" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="writescript" property="inputFile" value="<%=fileList.get(i)%>"/>
							<jsp:setProperty name="writescript" property="processName" value="seq-qc"/>
							<jsp:setProperty name="writescript" property="cmd" value="<%=prinSeq.doPrinSeqExecute()%>"/>
												
							<!-- Mapping -->
							<jsp:setProperty name="bwa" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="bwa" property="inputFile" value="<%=fileList.get(i)%>"/>
	
							<jsp:setProperty name="writescript" property="processName" value="mapping-bwa-aln"/>
							<jsp:setProperty name="writescript" property="cmd" value="<%=bwa.doBWAalnExecute()%>"/>					
							
							<jsp:setProperty name="writescript" property="processName" value="mapping-bwa-samse"/>
							<jsp:setProperty name="writescript" property="cmd" value="<%=bwa.doBWAsamseExecute()%>"/>
							
							<!-- Mapping-QC -->
							<jsp:setProperty name="samstat" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="samstat" property="inputFile" value="<%=fileList.get(i)%>"/>
	
							<jsp:setProperty name="writescript" property="processName" value="mapping-qc"/>
							<jsp:setProperty name="writescript" property="cmd" value="<%=samstat.doSamstatExecute()%>"/>
												
							<!-- Variant Call -->
							<jsp:setProperty name="samtools" property="realFolder" value="<%=saveFolder%>"/>
							<jsp:setProperty name="samtools" property="inputFile" value="<%=fileList.get(i)%>"/>
	
							<jsp:setProperty name="writescript" property="processName" value="vcall-samtools-bam"/>
							<jsp:setProperty name="writescript" property="cmd" value="<%=samtools.doSamtoolsBAMExecute()%>"/>
												
							<jsp:setProperty name="writescript" property="processName" value="vcall-samtools-sort"/>
							<jsp:setProperty name="writescript" property="cmd" value="<%=samtools.doSamtoolsSortExecute()%>"/>
							
							<jsp:setProperty name="writescript" property="processName" value="vcall-samtools-pileup"/>
							<jsp:setProperty name="writescript" property="cmd" value="<%=samtools.doSamtoolsPileupExecute()%>"/>
	
							<jsp:setProperty name="writescript" property="processName" value="vcall-samtools-snpcall"/>
							<jsp:setProperty name="writescript" property="cmd" value="<%=samtools.doSamtoolsSNPcallExecute()%>"/>

							<!-- Variant Annotation
							<font>5. Variant Annotation</font> &nbsp; <input type="button" name="vannotation" value="view"><br>													
							 -->
			<%
						}
			%>
						<!-- Running SGE scripts -->
						<!-- Seq-QC -->
						<jsp:setProperty name="runscript" property="realFolder" value="<%=saveFolder%>"/>
						<jsp:setProperty name="runscript" property="inputFiles" value="<%=fileList%>"/>
						<jsp:setProperty name="runscript" property="processName" value="seq-qc"/>
						<%SeqQCResult=runscript.doScript();%>

						<!-- Mapping -->
						<jsp:setProperty name="runscript" property="processName" value="mapping-bwa-aln"/>
						<%MappingBwaAlnResult=runscript.doScript();%>

						<jsp:setProperty name="runscript" property="processName" value="mapping-bwa-samse"/>
						<%MappingBwaSamseResult=runscript.doScript();%>

						<!-- Mapping-QC -->
						<jsp:setProperty name="runscript" property="processName" value="mapping-qc"/>
						<%MappingQCResult=runscript.doScript();%>

						<!-- Variant Call -->
						<jsp:setProperty name="runscript" property="processName" value="vcall-samtools-bam"/>
						<%VcallSamtoolsBamResult=runscript.doScript();%>

						<jsp:setProperty name="runscript" property="processName" value="vcall-samtools-sort"/>
						<%VcallSamtoolsSortResult=runscript.doScript();%>
						
						<jsp:setProperty name="runscript" property="processName" value="vcall-samtools-pileup"/>
						<%VcallSamtoolsPileupResult=runscript.doScript();%>
						
						<jsp:setProperty name="runscript" property="processName" value="vcall-samtools-snpcall"/>
						<%VcallSamtoolsSnpcallResult=runscript.doScript();%>						
						
			<%
						for (int i = 0; i < fileList.size() && i < originFileList.size(); i++) {
			%>
							<h3>Sequence : <%=fileList.get(i)%> / <%=originFileList.get(i)%></h3>
							
							<!-- Seq-QC -->
							<% if (SeqQCResult.equals("true")) {%>
										<font>1. Seq-QC</font> &nbsp; <input type="button" name="seqqc" value="view" onclick="onSeqQC()"> &nbsp;&nbsp;
							<% } else { %>
										<font>1. Seq-QC</font> &nbsp; <input type="button" name="seqqc" value="view" disabled="disabled"> &nbsp;&nbsp;
							<% } %>
	
							<!-- Mapping -->
							<%if (MappingBwaAlnResult.equals("true") && MappingBwaSamseResult.equals("true") ) {%>
										<font>2. Mapping</font> &nbsp; <input type="button" name="mapping" value="view"> &nbsp;&nbsp;
							<% } else { %>
										<font>2. Mapping</font> &nbsp; <input type="button" name="mapping" value="view" disabled="disable"> &nbsp;&nbsp;
							<% } %>
							
							<!-- Mapping-QC -->
							<% if (MappingQCResult.equals("true")) {%>
										<font>3. Mapping-QC</font> &nbsp; <input type="button" name="mappingqc" value="view"> &nbsp;&nbsp;
							<% } else { %>
										<font>3. Mapping-QC</font> &nbsp; <input type="button" name="mappingqc" value="view" disabled="disable"> &nbsp;&nbsp;
							<% } %>
							
							<!-- Variant Call -->
							<%if (VcallSamtoolsBamResult.equals("true") && VcallSamtoolsSortResult.equals("true") && 
									VcallSamtoolsPileupResult.equals("true") && VcallSamtoolsSnpcallResult.equals("true")) {%>
										<font>4. Variant Call</font> &nbsp; <input type="button" name="vcall" value="view"> &nbsp;&nbsp;
							<% } else { %>
										<font>4. Variant Call</font> &nbsp; <input type="button" name="vcall" value="view" disabled="disable"> &nbsp;&nbsp;
							<% } %>							
			<%									
						}						
					}	
				} catch (Exception e){
					e.printStackTrace();
				}
			%>
			</div>
			
			<div id="resultPanel"></div>
		<jsp:include page="../../bottom.jsp" flush="false"/>
	</body>
	<script type="text/javascript">
				function onSeqQC() {
					$("#resultPanel").append('<br><%=prinSeq.doPrinSeqExecute()%>');
				}
	</script>
</html>